package com.example.a321.rxnetdemo;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.orhanobut.logger.Logger;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Response;
import retrofit2.adapter.rxjava.HttpException;

public class BaseActivity extends AppCompatActivity {
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("加载中...");
    }

    protected void getData() {
        progressDialog.show();
    }

    protected void postData() {
        progressDialog.show();
    }

    protected void handlerSuccess(BaseEntity<? extends BaseEntity> entity) {
        progressDialog.cancel();
        Logger.d(entity.getResultcode());
    }

    protected void handlerFailure(Throwable throwable) {
        progressDialog.cancel();
        if (throwable instanceof HttpException) {
            HttpException httpException = (HttpException) throwable;
            Response response = httpException.response();
            ResponseBody responseBody = response.errorBody();
            try {
                Logger.d(response.code());
                Logger.d(responseBody.string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Logger.d(throwable);
    }

    protected void handlerPostSuccess(BaseEntity<? extends BaseEntity> entity) {
        progressDialog.cancel();
        Logger.d(entity.getResultcode());
    }

    protected void handlerPostFailure(Throwable throwable) {
        progressDialog.cancel();
        Logger.d(throwable);
    }
}
